/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.dao.DccrDAO;
import com.cashmate.hibernate.dao.PastDueListingDAO;
import com.cashmate.hibernate.pojo.Dccr;
import com.cashmate.hibernate.pojo.Employees;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Dean
 */
public class PostDccr extends ActionSupport {

    private InputStream inputStream;
    private Logger logger = Logger.getLogger(PostDccr.class);
    private final DccrDAO dccrdao = (DccrDAO) ServletActionContext.getServletContext().getAttribute("dccrDAO");
    private final Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private final BranchDAO branchdao = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private final PastDueListingDAO pastdao = (PastDueListingDAO) ServletActionContext.getServletContext().getAttribute("pastdao");
    private String method;

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    @Action(value = "/submit-dccr", results = {
        @Result(name = "success", type = "stream", params = {"inputName", "inputStream", "contentType", "text/html"})
    })
    @Override
    public String execute() throws Exception {
        try {
            Object ses_dccrdate = ServletActionContext.getRequest().getSession().getAttribute("dccrdate");
            Object ses_branch = ServletActionContext.getRequest().getSession().getAttribute("dccrbranch");
            if (method.equals("post") && ses_dccrdate != null && ses_branch != null) {


                DetachedCriteria dc = DetachedCriteria.forClass(Dccr.class);
                dc.add(Restrictions.eq("dategenerated", ses_dccrdate.toString()));
                dc.add(Restrictions.eq("branchid", Integer.parseInt(ses_branch.toString())));
                dc.add(Restrictions.eq("isactive", true));

                int records = dccrdao.countByCriteria(dc);

                dc.setProjection(null);
                dc.setResultTransformer(Criteria.ROOT_ENTITY);
                List<Dccr> dccrList = null;

                if (records > 0) {
                    dccrList = dccrdao.findByCriteria(dc, 0, records);
                    for (Dccr d : dccrList) {
                        dccrdao.deleteGroup(d.getDccrid());
                    }
                    dccrdao.commitGroup();
                }
                DetachedCriteria dc2 = DetachedCriteria.forClass(Dccr.class);

                dc2.add(Restrictions.eq("dategenerated", ses_dccrdate.toString()));
                dc2.add(Restrictions.eq("branchid", Integer.parseInt(ses_branch.toString())));
                dc2.add(Restrictions.eq("isactive", false));

                int records2 = dccrdao.countByCriteria(dc2);

                dc2.setProjection(
                        null);
                dc2.setResultTransformer(Criteria.ROOT_ENTITY);

                if (records2 > 0) {
                    dccrList = dccrdao.findByCriteria(dc2, 0, records2);
                    for (Dccr d : dccrList) {
                        d.setIsactive(true);
                        dccrdao.updateGroup(d);
                    }
                    dccrdao.commitGroup();
                    
                    pastdao.pastDueProcedure(ses_dccrdate.toString(), Integer.parseInt(ses_branch.toString()));
                }

                ServletActionContext.getRequest().getSession().removeAttribute("dccrdate");
                ServletActionContext.getRequest().getSession().removeAttribute("dccrbranch");
                ServletActionContext.getRequest().getSession().removeAttribute("dccrgendate");
                ServletActionContext.getRequest().getSession().removeAttribute("dccrbranchname");

                logger.info(
                        "Employee " + emps.getIdnumber() + " posted DCCR for " + ses_dccrdate + " at "
                        + branchdao.get(Integer.parseInt(ses_branch.toString())).getBranchname());

                inputStream = new StringBufferInputStream("<script type=\"text/javascript\">firewarn=false;window.location = \"dccr.jsp\";</script>");
            } else if (method.equals("cancel") && ses_dccrdate != null && ses_branch != null) {

                DetachedCriteria dc = DetachedCriteria.forClass(Dccr.class);
                dc.add(Restrictions.eq("dategenerated", ses_dccrdate.toString()));
                dc.add(Restrictions.eq("branchid", Integer.parseInt(ses_branch.toString())));
                dc.add(Restrictions.eq("isactive", false));

                int records = dccrdao.countByCriteria(dc);

                dc.setProjection(null);
                dc.setResultTransformer(Criteria.ROOT_ENTITY);

                if (records
                        > 0) {
                    List<Dccr> dccrList = dccrdao.findByCriteria(dc, 0, records);
                    for (Dccr d : dccrList) {
                        dccrdao.deleteGroup(d.getDccrid());
                    }
                    dccrdao.commitGroup();
                }
//                List<Dccr> debitList = (List<Dccr>) ServletActionContext.getRequest().getSession().getAttribute("debitList");
//
//                if (debitList != null) {
//                    ServletActionContext.getRequest().getSession().removeAttribute("debitList");
//                }

                ServletActionContext.getRequest().getSession().removeAttribute("dccrdate");
                ServletActionContext.getRequest().getSession().removeAttribute("dccrbranch");
                ServletActionContext.getRequest().getSession().removeAttribute("dccrgendate");
                ServletActionContext.getRequest().getSession().removeAttribute("dccrbranchname");

                inputStream = new StringBufferInputStream("<script type=\"text/javascript\">firewarn=false;window.location= \"dccr.jsp\";</script>");
            } else {
                return NONE;
            }
        } catch (Exception ex) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + ex.getLocalizedMessage(), ex);
            } else {
                logger.fatal(ex.getLocalizedMessage(), ex);
            }
            ex.printStackTrace();
            addActionError(ex.getLocalizedMessage());
            return ERROR;
        } finally {
            DccrDAO.close();
            BranchDAO.close();
            PastDueListingDAO.close();
        }
        return SUCCESS;
    }
}